package com.jiwei.system.controller;


import com.jiwei.system.pojo.MemberLogin;
import com.jiwei.system.pojo.Task;
import com.jiwei.system.utils.CookieUtil;
import com.jiwei.system.utils.JWTUtil;
import com.jiwei.system.serviceImpl.RepairServiceImpl;
import com.jiwei.system.vo.Login;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RequestMapping(value = "repair", method = RequestMethod.POST)
@Validated
@RestController
public class UserActionController {
    @Resource(name = "RepairService")
    private RepairServiceImpl repairService;

    @PostMapping(value = "sendRepairForm")
    public String acceptRepair(Task task,HttpServletRequest request){
//        String token = CookieUtil.getCookieValueNamedToken(request);
//        String phoneNumber = JWTUtil.getClaimNamedAccount(token);
//        Login userlogin = (Login) request.getSession().getAttribute("login");
        String phoneNumber = (String)request.getSession().getAttribute("userlogin");
        task.setTelephone(phoneNumber);
        task.setStatus("未处理");
        String result = repairService.acceptRepair(task);
        if(result=="图片太大"){
            return "{" +
                    "\"statusCode\":\"" + 400 + '\"' +
                    ", \"message\":\"" + "图片太大" + "\""+
                    '}';
        }else if(result=="上传失败"){
            return "{" +
                    "\"statusCode\":\"" + 405 + '\"' +
                    ", \"message\":\"" + "上传失败" + "\""+
                    '}';
        }else{
            return "{" +
                        "\"statusCode\":\"" + 200 + '\"' +
                    ", \"message\":\"" + "报修成功" + "\""+
                    '}';

        }

    }


    @PostMapping(value = "getHistoryRepairForms")
    public Map<String,Object> sendHistoryRepairForm(HttpServletRequest request){
//        String token = CookieUtil.getCookieValueNamedToken(request);
//        String phoneNumber = JWTUtil.getClaimNamedAccount(token);
//        Login userlogin = (Login) request.getSession().getAttribute("login");
        String phoneNumber = (String)request.getSession().getAttribute("userlogin");
        Map<String,Object> res = new HashMap<>();
        res.put("code", 0);
        res.put("count", repairService.queryUserTaskNumsByPhoneNumber((phoneNumber)));
        res.put("data", repairService.userQuerySelfTask(phoneNumber));
        return res;
    }

    @PostMapping(value = "cancelRepairForm")
    public String cancelRepair(@RequestParam("id") int id, HttpServletRequest request){
//        String token = CookieUtil.getCookieValueNamedToken(request);
//        String phoneNumber = JWTUtil.getClaimNamedAccount(token);
//        Login userlogin = (Login) request.getSession().getAttribute("login");
        String phoneNumber = (String)request.getSession().getAttribute("userlogin");
        repairService.canceluserTask(id,"取消",phoneNumber,"未处理");
        return "{" +
                "\"statusCode\":\"" + 200 + '\"' +
                ", \"message\":\"" + "取消成功" + "\""+
                '}';

    }

    @PostMapping(value = "usergetTaskDetail")
    public Map<String,Object> sendRepairDetail(@RequestParam("id") int id, HttpServletRequest request){
//        String token = CookieUtil.getCookieValueNamedToken(request);
        String phoneNumber = (String)request.getSession().getAttribute("userlogin");
//        Login userlogin = (Login) request.getSession().getAttribute("login");
//        String phoneNumber = userlogin.getAccount();

        Map<String,Object> res = new HashMap<>();
        res.put("code", 0);
        res.put("data", repairService.usergetTaskDetail(id,phoneNumber));
        return res;
    }

}
